import { Component, OnInit, Input, EventEmitter, Output } from '@angular/core';
import { Http } from '@angular/http';
import { Service } from 'app/server/server';
import { DoCheck } from '@angular/core/src/metadata/lifecycle_hooks';

@Component({
  selector: 'app-product-terminal',
  templateUrl: './product-terminal.component.html',
  styleUrls: ['./product-terminal.component.css']
})
export class ProductTerminalComponent implements OnInit, DoCheck {
  ngDoCheck(): void {
    this.dataChange.emit(this._data)
  }
  terminaLists: Array<any> = []
  _data: Array<any> = [new Terminal()]
  @Output() dataChange: EventEmitter<Array<Terminal>> = new EventEmitter();
  constructor(
    private http: Http,
    private srv: Service
  ) { }

  ngOnInit() {
    this.http.post(this.srv.baseUrl + 'product/terminals/getList', {
      "pageNumber": 1,
      "pageSize": 50,
      "queryType": 3,
      "queryCriteria": ""
    }).subscribe(res => {
      if (res.json().result_code == "0000") {
        this.terminaLists = res.json().result
      } else {
        //console.error(`${res.json().result_code}:${res.json()}`)
      }
    }, err => {
      console.error("product-terminal.component.ts获取终端信息失败")
      //console.error(err)
    })
  }
  addtr() {
    this._data.push(new Terminal())
  }
  deleteTr(i: number) {
   // console.log(i)
    if (this._data.length > 1) {
      this._data.splice(i, 1)
    }
  }
  @Input() set data(value) {
    if (value instanceof Array) {
      if (value.length) {
        this._data = value
      } else {
        this._data = [new Terminal()]
      }
    } else {
      console.error("product-terminal.component input属性data必须是[]")
      // this._data = [new Terminal()]
    }
  }
  @Input() edited: boolean = true;
  @Input() expressionError:boolean = false; // 是否提示用户输入
}
class Terminal {
  drmPassword: string;
  drmId: string;
  password: string;
  passwordId: string;
  uri: any;
  uriType: any;
  terminalCode: any;

  constructor(
    terminalCode: string = "",
    uri: string = "",
    uriType: string = "",
    passwordId: string = "",
    password: string = "",
    drmId: string = "",
    drmPassword: string = "",
  ) {
    this.terminalCode = terminalCode || "";
    this.uri = uri
    this.uriType = uriType
    this.passwordId = passwordId
    this.password = password
    this.drmId = drmId
    this.drmPassword = drmPassword
  }
}
